We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR)

Controller

<?php
public function indexAction()
    {
        $item = new \stdClass();
        $item->var1 = '1';
        $item->var2 = '2';
        $item->var4 = '3';
        $test = array(
            'item' =>  $item
        );
        $this->view->setVars(array(
            'test' => $test
        ));
    }

Volt template

{{ dump(test['item'].var1) }}

Code generated in cache

<?php echo var_dump(($test['item'])->var1); ?>

Volt template

{{ test['item'].var1 }}

Code generated in cache

<?php echo ($test['item'])->var1; ?>

I get ( ! ) Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ',' or ';' in /work/nested-set/tree/app/cache/_work_nested-set_tree_app_views_index_index.volt.php on line 26

In Controller all works as usual


var_dump($test['item']->var1); //1

My question is there is a bug or my mistake?

This structure works as expected

{% for t in tree %}
    {% set a = t["item"] %}
    {{ a.getName() }}
{% endfor %}

But why do I have create another variable for getting object property?

Thanks



2.5k

Hi,

As far as I know, it is not possible to wrap an object and try to access its property like this :

($test['item'])->var1;

That is why the generated code get a parse error. I don't know why the code generate like this.

With the structure you show :

{% for t in tree %}
    {% set a = t["item"] %}
    {{ a.getName() }}
{% endfor %}

You will get something like :

$a = $t['item'];
echo $a->getName();

And not ($t["item"])->getName();, so it will work.



12.2k
edited Feb '15

Yeap.

You are right. Probably my question was not clear. Because English is not my native language.

This code placed in volt's cache file. in case when I print in Volt's template {{ test['item'].var1 }}

echo ($test['item'])->var1;

I did not print it by my fingers )))



33.8k

b4v is right. You need to set a var that is one of your array's items, no other way to do it in Volt. That happened to me time ago.



12.2k

Thanks you all. Seems like it's a bug. Very sad that we have to generate extra variables for that.